import store from '@/store/index.js'
import api from '@/common/js/util.js'

// 登录页面
const loginPage = "/pages/login/login"

// 鉴权页面 路径以'/'开头
const authPageList = []

// 检查地址白名单
function checkPage(url) {
	const path = url.split('?')[0]
	return authPageList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(to) {
			//如果是鉴权页面进行拦截验证
			if (checkPage(to.url)) {
				if (store.getters.loginStatus) {
					return true
				} else {
					api.navigateTo(loginPage)
					return false
				}
			}
		},
		fail(err) {
			console.log(err)
		}
	})
})
